package com.cxz.kotlin.baselibs.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import java.util.HashMap;
import java.util.List;


/**
 * recycleview可以实现多重布局adapter
 * @param <T>
 */
public abstract class MultiViewRecycleAdapter<T> extends RecyclerView.Adapter<MultiViewHolder> {


    public static final int TYPE_ITEM_ONE_LIST = 0;//单列
    public static final int TYPE_ITEM_THREE_GRI = 2;//三列
    public static final int TYPE_ITEM_FOUR_GRI = 1;//四列

    public static final int TYPE_MYPROFILE_PHOTO = 3;//个人中心头像
    public static final int TYPE_MYPROFILE_NORMAL = 4;//个人中心normalitem
    public static final int TYPE_SETTING_LOGOUT = 5;//设置页面退出登录
    public static final int TYPE_SETTING_ITEM_WITH_PHOTO = 6;//带图片的item


    public static final int TYPE_LL_VERTICAL_FOR_ADD = 7;//垂直
    public static final int TYPE_LOGISTICS_TRACKING = 8;//带图片的item


    public static final int TYPE_KEY_VALUE_NO_PICS = 9;//不带图片的item
    public static final int TYPE_TEXT_ONLY = 10;//纯文字的item
    public static final int TYPE_TEXT_DESCRIBE_WITH_DRAWABLE = 11;//带图片的item


    public static final int TYPE_TWO_BUTTON = 12;//双按钮
    public static final int TYPE_ONE_BUTTON = 13;//单按钮
    public static final int TYPE_KEY_VALUE_INPUT= 14;//key value输入框
    public static final int TYPE_CHECKBOX_TEXT= 15;//checkbox ITEM
    public static final int TYPE_EMPTY= 16;//空数据

    public static final int TYPE_1= 17;//空数据
    public static final int TYPE_2 =18;//空数据
    public static final int TYPE_3= 19;//空数据

    public static final int TYPE_ITEM_ONE_LIST_1 = 20;//单列
    public static final int TYPE_ITEM_ONE_LIST_2 = 21;//单列

    public static final int HOME_TYPE_BANNER = 22;//banner轮播
    public static final int HOME_TYPE_STROES = 23;//线下门店
    public static final int HOME_TYPE_SECKILL = 24;//秒杀
    public static final int HOME_TYPE_IAMGE = 25;//图片模式
    public static final int HOME_TYPE_GOOD_LIST = 26;//商品列表

    public static final int TYPE_ITEM_TWO_GRI = 27;//两列


    public static final int HOME_TYPE_DISCOVER_GOOD_LIST = 28;//精选达人推荐
    public static final int HOME_TYPE_CATALOG = 29;//分类
    public static final int HOME_TYPE_BEST_SELLER = 30; //热销榜
    public static final int HOME_TYPE_MERCHANTS_ANNOUNCEMENT = 31; //商家公告




    private final LayoutInflater mInflater;
    private final List<T> mDatas;
    private final HashMap<Integer, Integer> mlayoutIdsMap;
    private Context mContext;


    /**
     *
     * @param context
     * @param layoutIds hashmap存储viewtype与layoutId
     * @param datas
     */
    public MultiViewRecycleAdapter(Context context, HashMap<Integer, Integer> layoutIds, List<T> datas) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mlayoutIdsMap = layoutIds;
        mDatas = datas;
    }



    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            GridLayoutManager gridManager = ((GridLayoutManager) manager);

            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    Log.i("@@","type = "+type) ;
                    switch (type) {
//                        case TYPE_ITEM_ONE_LIST:叠加可以实现一列多布局，以及多列中的每item布局
                        case TYPE_ITEM_ONE_LIST:
                        case TYPE_ITEM_ONE_LIST_1:
                        case TYPE_ITEM_ONE_LIST_2:
                            return 6;
                        case TYPE_ITEM_THREE_GRI:
                            return 2;//3列
                        case TYPE_ITEM_TWO_GRI:
                            return 3;//2列
                        case TYPE_ITEM_FOUR_GRI:
                            return 2;//4列
                        default:
                            return 6;
                    }
                }
            });
        }

    }

    @NonNull
    @Override
    public MultiViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        int viewId = mlayoutIdsMap.get(viewType);
        MultiViewHolder viewHolder = MultiViewHolder.getRecyclerHolder(mContext,parent, viewId);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull MultiViewHolder holder, int position) {
        convert(holder, mDatas.get(position), position,getItemViewType(position));
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    protected int getPosition(RecyclerView.ViewHolder viewHolder) {
        return viewHolder.getAdapterPosition();
    }

    public abstract void convert(MultiViewHolder holder, T bean, int position, int viewType);

    @Override
    public int getItemViewType(int position) {
        return getCustomItemViewType(position);
    }


    public abstract int getCustomItemViewType(int position);


    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public List<T> getData() {
        return mDatas;
    }


}
